#ifndef _ADC_H_
#define _ADC_H_
#include "opcje.h"
#include "buttons.h"

unsigned char ADC_value;

// --------------
// ---- ADC -----
// --------------

inline void ADC_init()
{
	ADMUX |= (1<<REFS0) | (1<<ADLAR);
	ADCSRA = (1<<ADEN) | (1<<ADFR) | (1<<ADPS2) | (1<<ADPS0) | (1<<ADIE);
}

inline void ADC_startMeasure()
{
	ADCSRA |= (1<<ADSC);
}

void ADC_stopMeasure()
{
	ADCSRA &= ~(1<<ADFR);
	_delay_ms(10);
	ADCSRA |= (1<<ADFR);
}

unsigned char ADC_measure()
{
	ADCSRA |= (1<<ADSC);// | (1<<ADIE);
	//_delay_ms(20);
	while ( !( ADCSRA & (1<<ADIF ) ) )
	{
		asm("NOP");
	}
	ADCSRA &= ~(1<<ADIF);
	return ADCH;
}

ISR(ADC_vect)
{
		ADC_value = ADCH;
}

#endif
